package xyz.xpman.prisonerfitness.service;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import org.jetbrains.annotations.NotNull;

import xyz.xpman.prisonerfitness.dao.ILocalCache;
import xyz.xpman.prisonerfitness.dao.XmlLocalCache;
import xyz.xpman.prisonerfitness.entity.User;
import xyz.xpman.prisonerfitness.util.StrKit;


@RequiresApi(api = Build.VERSION_CODES.O)
public class UserService {
    private static User DAO;
    @SuppressLint("StaticFieldLeak")
    public static UserService ME = null;
    @SuppressLint("StaticFieldLeak")
    private static boolean init = false;
    private static ILocalCache localCache = null;


    private UserService() { }


    public static void init(@Nullable Context context) {
        if (!init && context != null) {

            User.init(context);

            DAO = new User();
            ME = new UserService();

            localCache = new XmlLocalCache(context, (id) -> DAO.findById(id));

            init = true;
        }
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    /**
     * 保存用户名和密码
     *
     * @param user 用户
     * @return 是否保存成功
     */
    public String register(@NotNull User user) {
        String message = user.checkForAdd();

        if (StrKit.notBlank(message)) {
            return "注册失败: " + message;
        }

        if (null != DAO.findByUsername(user.getUsername())) {
            return "注册失败: 该用户名已被使用!";
        }

        if (user.add()) {
            return "";
        }
        return "注册失败!";
    }


    @SuppressLint("CommitPrefEdits")
    public String loginByUsernameAndPassword(User user) {

        if (StrKit.isBlank(user.getUsername())) {
            return "用户名不能为空!";
        }

        if (StrKit.isBlank(user.getPassword())) {
            return "密码不能为空!";
        }

        if (user.loginByUsernameAndPassword()) {
            if (saveLoggedInUserId(user.getId())) {
                return "";
            }
            return "系统错误: 保存登录状态失败!";
        }
        return "登录失败: 用户名或密码错误!";
    }


    public void deleteNotRoot() {
        DAO.deleteNotRoot();
    }

    public boolean logout() {
        return removeLoggedInUserId();
    }


    // sp - bean - user 相关
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    /*public boolean addToSp(User user) {
        return BeanUtil.saveBean2Sp(DEFAULT_CONTEXT, user, userConfigFileName, user.getUsername());
    }


    public User getBySp(String username) {
        return BeanUtil.getBeanFromSp(DEFAULT_CONTEXT, userConfigFileName, username);
    }*/


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


/*
    @SuppressLint("WrongConstant")
    private SharedPreferences getSharedPreferences() {
        return DEFAULT_CONTEXT.getSharedPreferences(userConfigFileName, Context.MODE_PRIVATE);
    }

    private SharedPreferences getSp() {
        return getSharedPreferences();
    }

    private SharedPreferences.Editor getSpEdit() {
        return getSharedPreferences().edit();
    }
*/


    // /data/user/0/com.domain.subdomain/shared_prefs/user_mes.xml
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    private boolean saveLoggedInUserId(int id) {
        return localCache.saveLoggedInUserId(id);
    }

    private boolean removeLoggedInUserId() {
        return localCache.removeLoggedInUserId();
    }

    @SuppressWarnings("unused")
    private Integer getLoggedInUserId() {
        return localCache.getLoggedInUserId();
    }

    public boolean isUserLoggedIn() {
        return null != getCurrentUser();
    }

    public User getCurrentUser() {
        return localCache.getCurrentUser();
    }

    public Integer getCurrentUserId() {
        return localCache.getLoggedInUserId();
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



}
